package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.exception.BssException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @author 付喆
 * @version 2021/5/25 11:56
 * Description: 固定带宽
 */
public class FixedBandwidthHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    /**
     * VC12
     */
    public static final String VC12 = "110007242";

    /**
     * VC3
     */
    public static final String VC3 = "110007243";

    /**
     * VC4
     */
    public static final String VC4 = "110007244";

    /**
     * VC捆绑方式
     */
    public static final String VC_TYPE = "210004194";

    /**
     * VC捆绑数量
     */
    public static final String VC_NUM = "210004195";

    /**
     * 固定带宽
     */
    private static final String FIXED_BAND_WIDTH = "210004224";

    @Autowired
    IQuerySceneInstService querySceneInstService;


    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        super.before(attr, inst, value, data);
        if(data.getTriggerStrategy() != null) {
            // 初始化加载页面时不校验，跳过初次加载时的属性 set 触发
            return;
        }
        if (KiteStringUtils.isEmpty(value)) {
            return;
        }
        String serviceOfferId = data.getServiceOfferId();
        if ("2826".equals(serviceOfferId)) {
            // 产品变更 校验升速、降速变更
            List<String> serviceOfferIdList = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "BUSI_CHANGE_SERVICEOFFERID");
            String originValueName = getValueName(attr, attr.getOriginValue());
            String currentValueName = getValueName(attr, value);
            if(StringUtils.isBlank(originValueName) || StringUtils.isBlank(currentValueName) || Objects.isNull(serviceOfferIdList)){
                return;
            }
            int originUnit = originValueName.endsWith("G") ? 1024 : 1;
            int currentUnit = currentValueName.endsWith("G") ? 1024 : 1;
            double current = Double.parseDouble(currentValueName.replaceAll("[^0-9\\.]", "")) * currentUnit;
            double origin = Double.parseDouble(originValueName.replaceAll("[^0-9\\.]", "")) * originUnit;
            if (serviceOfferIdList.contains("1093")){
                // 升速
                if(origin > current) {
                    BssException.throwOut("", "","升速带宽新值不能小于原始带宽值");
                }
            } else if (serviceOfferIdList.contains("1094")){
                // 降速
                if(origin < current) {
                    BssException.throwOut("", "","降速带宽新值不能大于原始带宽值");
                }
            }
        }
        List<KiteAttrValue> attrValues = attr.getAttrValues();
        String valueDesc =null;
        for(KiteAttrValue kiteAttrValue:attrValues){
            if(KiteStringUtils.equals(kiteAttrValue.getAttrValue(),value)){
                valueDesc = kiteAttrValue.getAttrValueName();
            }
        }
        checkVcValue(Optional.ofNullable(inst.getAttr(VC_TYPE)).orElseGet(KiteAttr::new).getValue(),Optional.ofNullable(inst.getAttr(VC_NUM)).orElseGet(KiteAttr::new).getValue(),valueDesc);
    }

    /**
     * VC捆绑方式校验
     * @param attr1 VC捆绑方式
     * @param attr2 VC捆绑数量
     * @param attr3 固定带宽
     * @return
     */
    public static void checkVcValue(String attr1, String attr2, String attr3){
        // VC12=2M、VC3=45M、VC4=155M

        //110007242(VC12)
        //110007243(VC3)
        //110007244(VC4)
        Map<String, String> config = new HashMap<>(18);
        config.put(VC12,"2");
        config.put(VC3,"45");
        config.put(VC4,"155");
        if(KiteStringUtils.isNotEmpty(attr1)&&KiteStringUtils.isNotEmpty(attr2)&&KiteStringUtils.isNotEmpty(attr3)){
            String attrString1 = config.get(attr1);
            if(KiteStringUtils.isNotEmpty(attrString1)){
                double attrFloat1 = Double.parseDouble(attrString1);
                double attrFloat2 = Double.parseDouble(attr2);
                int originUnit = attr3.endsWith("G") ? 1024 : 1;
                double attrFloat3 = Double.parseDouble(attr3.replaceAll("[^0-9\\.]", "")) * originUnit;
                if(attrFloat1*attrFloat2 < attrFloat3){
                    BssException.throwOut("", "","VC捆绑方式*VC捆绑数量的值应该大于等于固定带宽");
                }
            }
        }
    }

    private static String getValueName (KiteAttr attr, String OriginValue){
        for(KiteAttrValue kiteAttrValue : attr.getAttrValues()) {
            if(kiteAttrValue.getAttrValue().equalsIgnoreCase(OriginValue)) {
                return kiteAttrValue.getAttrValueName();
            }
        }
        return null;
    }
}
